package com.cyy.cn.controller;



import com.cyy.cn.pojo.Spit;
import com.cyy.cn.service.SpitService;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;




import entity.Result;
import entity.StatusCode;
/**
 * spit控制器层
 * @author Administrator
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/spit")
public class SpitController {

	@Autowired
	private SpitService spitService;

	@Autowired
	private RedisTemplate redisTemplate;
	
	/**
	 * 查询全部数据
	 * @return
	 */
	@RequestMapping(method= RequestMethod.GET)
	public Result findAll(){
		return new Result(true,StatusCode.OK,"查询成功",spitService.findAll());
	}
	
	/**
	 * 根据ID查询
	 * @param id ID
	 * @return
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.GET)
	public Result findById(@PathVariable String id){
		return new Result(true,StatusCode.OK,"查询成功",spitService.findById(id));
	}


	
	/**
	 * 增加
	 * @param spit
	 */
	@RequestMapping(method=RequestMethod.POST)
	public Result add(@RequestBody Spit spit  ){
		spitService.add(spit);
		return new Result(true,StatusCode.OK,"增加成功");
	}
	
	/**
	 * 修改
	 * @param spit
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.PUT)
	public Result update(@RequestBody Spit spit, @PathVariable String id ){
		spit.set_id(id);
		spitService.update(spit);
		return new Result(true,StatusCode.OK,"修改成功");
	}
	
	/**
	 * 删除
	 * @param id
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.DELETE)
	public Result delete(@PathVariable String id){
		spitService.deleteById(id);
		return new Result(true,StatusCode.OK,"删除成功");
	}


	/*@RequestMapping(value="/updateState/{id}",method = RequestMethod.GET)
	public Result updateData(@PathVariable String id){
		spitService.updateState(id);
		return new Result(true,StatusCode.OK,"审核成功");
	}*/

	/**
	 * 根据parentId查询上级
	 * @param parentId
	 * @return
	 */
	@RequestMapping(value="/parent/{parentId}/{page}/{size}",method = RequestMethod.GET)
	public Result updateThumbup(@PathVariable String parentId,@PathVariable int page,@PathVariable int size){
		Page<Spit> pageList = spitService.findByParentId(parentId, page, size);
		return new Result(true,StatusCode.OK,"查询成功",new PageResult<Spit>(pageList.getTotalElements(),pageList.getContent()));
	}

	/**
	 * 吐槽点赞
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/thumbup/{id}",method = RequestMethod.GET)
	public Result updateThumbup(@PathVariable String id){
		String userid = "1013";
		if(redisTemplate.opsForValue().get("thumbup_"+userid + "_" + id) != null){
			return new Result(false,StatusCode.REPERROR,"您已经点过赞了！");
		}
		spitService.updateThumbup(id);
		redisTemplate.opsForValue().set("thumbup_" + userid + "_" + id,"1");
		return new Result(true,StatusCode.OK,"点赞成功");
	}
}
